package com.example.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dw.entity.UserExam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户考试Mapper接口
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface UserExamMapper extends BaseMapper<UserExam> {

    /**
     * 根据用户ID获取所有考试记录
     */
    @Select("SELECT ue.*, e.exam_name, ct.courseType as courseTypeName " +
            "FROM user_exams ue " +
            "LEFT JOIN exams e ON ue.exam_id = e.exam_id " +
            "LEFT JOIN course_type ct ON ue.courseTypeid = ct.courseTypeid " +
            "WHERE ue.userid = #{userid} " +
            "ORDER BY ue.createdAt DESC")
    List<UserExam> getAllExamsByUser(@Param("userid") Integer userid);

    /**
     * 根据用户ID获取未完成的考试记录
     */
    @Select("SELECT ue.*, e.exam_name, ct.courseType as courseTypeName " +
            "FROM user_exams ue " +
            "LEFT JOIN exams e ON ue.exam_id = e.exam_id " +
            "LEFT JOIN course_type ct ON ue.courseTypeid = ct.courseTypeid " +
            "WHERE ue.userid = #{userid} AND ue.status = 0 " +
            "ORDER BY ue.createdAt DESC")
    List<UserExam> getUnfinishedExamsByUser(@Param("userid") Integer userid);

    /**
     * 根据用户ID和考试ID获取考试记录
     */
    @Select("SELECT ue.*, e.exam_name, ct.courseType as courseTypeName " +
            "FROM user_exams ue " +
            "LEFT JOIN exams e ON ue.exam_id = e.exam_id " +
            "LEFT JOIN course_type ct ON ue.courseTypeid = ct.courseTypeid " +
            "WHERE ue.userid = #{userid} AND ue.exam_id = #{examId}")
    UserExam getUserExam(@Param("userid") Integer userid, @Param("examId") Integer examId);

    /**
     * 根据用户ID和课程类型ID获取考试记录
     */
    @Select("SELECT ue.*, e.exam_name, ct.courseType as courseTypeName " +
            "FROM user_exams ue " +
            "LEFT JOIN exams e ON ue.exam_id = e.exam_id " +
            "LEFT JOIN course_type ct ON ue.courseTypeid = ct.courseTypeid " +
            "WHERE ue.userid = #{userid} AND ue.courseTypeid = #{courseTypeid} " +
            "ORDER BY ue.createdAt DESC")
    List<UserExam> getUserExamsByCourseType(@Param("userid") Integer userid, 
                                            @Param("courseTypeid") Integer courseTypeid);

    /**
     * 根据用户ID和课程类型ID分页获取考试记录
     */
    @Select("SELECT ue.*, e.exam_name, ct.courseType as courseTypeName " +
            "FROM user_exams ue " +
            "LEFT JOIN exams e ON ue.exam_id = e.exam_id " +
            "LEFT JOIN course_type ct ON ue.courseTypeid = ct.courseTypeid " +
            "WHERE ue.userid = #{userid} AND ue.courseTypeid = #{courseTypeid} " +
            "ORDER BY ue.createdAt DESC LIMIT #{offset}, #{size}")
    List<UserExam> getUserExamsByCourseTypePage(@Param("userid") Integer userid,
                                                @Param("courseTypeid") Integer courseTypeid,
                                                @Param("offset") Integer offset,
                                                @Param("size") Integer size);

    /**
     * 根据用户ID和课程类型ID获取未完成的考试记录
     */
    @Select("SELECT ue.*, e.exam_name, ct.courseType as courseTypeName " +
            "FROM user_exams ue " +
            "LEFT JOIN exams e ON ue.exam_id = e.exam_id " +
            "LEFT JOIN course_type ct ON ue.courseTypeid = ct.courseTypeid " +
            "WHERE ue.userid = #{userid} AND ue.courseTypeid = #{courseTypeid} AND ue.status = 0 " +
            "ORDER BY ue.createdAt DESC")
    List<UserExam> getUnfinishedExamsByCourseType(@Param("userid") Integer userid, 
                                                  @Param("courseTypeid") Integer courseTypeid);

    /**
     * 更新考试状态和成绩
     */
    @Update("UPDATE user_exams SET status = #{status}, score = #{score}, completed_at = NOW() " +
            "WHERE userid = #{userid} AND exam_id = #{examId}")
    int updateExamResult(@Param("userid") Integer userid, 
                        @Param("examId") Integer examId, 
                        @Param("status") Integer status, 
                        @Param("score") Float score);

    /**
     * 检查用户是否可以参加某个考试（是否已经完成）
     */
    @Select("SELECT COUNT(*) FROM user_exams WHERE userid = #{userid} AND exam_id = #{examId} AND status = 1")
    Integer checkExamCompleted(@Param("userid") Integer userid, @Param("examId") Integer examId);

    /**
     * 获取用户考试总数
     */
    @Select("SELECT COUNT(*) FROM user_exams WHERE userid = #{userid} AND courseTypeid = #{courseTypeid}")
    Integer getUserExamCountByCourseType(@Param("userid") Integer userid, 
                                        @Param("courseTypeid") Integer courseTypeid);
} 